package com.rongshu.view.discovery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rongshu.R;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.penpal.bean.PenpalArticleResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserPostAdapter extends RecyclerView.Adapter<UserPostAdapter.CollectedSerializeViewHolder> {

    private AdapterListener adapterListener;
    private Context mContext;
    private List<PenpalArticleResponse.ResponseBean> collectedArticles;

    public UserPostAdapter(Context context, List<PenpalArticleResponse.ResponseBean> collectedArticles) {
        this.mContext = context;
        this.collectedArticles = collectedArticles;
    }

    public void setOnItemClickListener(AdapterListener adapterListener){
        this.adapterListener = adapterListener;
    }

    @Override
    public CollectedSerializeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_user_article_item_view, parent, false);
        return new CollectedSerializeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(CollectedSerializeViewHolder holder, final int position) {
        holder.articleImg.setVisibility(TextUtils.isEmpty(collectedArticles.get(position).getTopImg()) ? View.GONE : View.VISIBLE);
        GlideUtils.loadImageView(mContext, holder.articleImg, collectedArticles.get(position).getTopImg(),
                R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        holder.articleTitle.setVisibility(TextUtils.isEmpty(collectedArticles.get(position).getTitle()) ? View.GONE : View.VISIBLE);
        holder.articleTitle.setText(collectedArticles.get(position).getTitle());
        try {
            String content = collectedArticles.get(position).getContents_header().replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            holder.articleContent.setText(URLDecoder.decode(content, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.creatDate.setText(DateUtils.ConvertDate(collectedArticles.get(position).getCreate_datetime()));
        holder.praseNum.setText("点赞" + collectedArticles.get(position).getLikeCount());
        holder.commentNum.setText("评论" + collectedArticles.get(position).getCommentCount());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterListener != null){
                    adapterListener.callBack(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return collectedArticles.size();
    }

    public static class CollectedSerializeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.article_img)
        ImageView articleImg;
        @BindView(R.id.article_title)
        TextView articleTitle;
        @BindView(R.id.article_content)
        TextView articleContent;
        @BindView(R.id.creat_date)
        TextView creatDate;
        @BindView(R.id.prase_num)
        TextView praseNum;
        @BindView(R.id.comment_num)
        TextView commentNum;

        public CollectedSerializeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
